<?php
class EmbedData extends CActiveRecord{
    
    public function getAllData($user_id,$currentPage, $numberRecordPerPage){
        $cacheService = new CacheService("EmbedData","getAllData",$currentPage);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
         
        if ($cache == false) 
        {
            if(intval($user_id)==0){
                $str_order = " ORDER BY order_embed ASC";
            }else{
                $str_order = " ORDER BY id DESC"; 
            }
            $connect =Yii::app()->db;
            $sql="SELECT id,user_id,title FROM c_embed_data WHERE user_id = ".intval($user_id)." AND status=1 ".$str_order." LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();
            Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_900);
        }
        else
        {
            $rows=$cache;
        }
        
        return $rows;
    }

    public function getDataById($id,$user_id){
        $cacheService = new CacheService("EmbedData","getDataById",$id,$user_id);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if ($cache == false) 
        {
            $connect =Yii::app()->db;

            $sql="SELECT id,user_id,title FROM c_embed_data WHERE status=1 AND id = ".intval($id)." AND user_id = ".intval($user_id);
            $command=$connect->createCommand($sql);
            $rows = $command->queryRow();
            Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_900);
        }
        else
        {
            $rows=$cache;
        }
        return $rows;
    }
    
     public function countTotalEmbed($user_id)
        {
            $sql = "SELECT count(id) FROM c_embed_data WHERE  user_id = ".intval($user_id);
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        
    public function getDataDetailByEmbedId($embed_id){
        $cacheService = new CacheService("EmbedData","getDataDetailByEmbedId",$embed_id);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        $type_display = 0;
        $layout = array();
        $item = array();
        if ($cache == false) 
        {
            $connect =Yii::app()->db;

            $sql="SELECT * FROM c_embed_data_detail WHERE embed_id = ".$embed_id;
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();
            foreach($rows as $key=>$value){
                $type_display = $value["type_display"];
                $layout = $value["layout"];
                if($value["catId"] >0){
                    switch($value["type_cat"]){
                        case 1:// game
                            $item[$value["catId"].'_'.$value["dataId"]] = Game::getGameById($value["dataId"]);
                            $item[$value["catId"].'_'.$value["dataId"]]["type"] = 1;
                            
                            break;
                      /*  case 2://video
                            $item[$value["catId"].'_'.$value["dataId"]] = Video::getVideoById($value["dataId"]);
                            $item[$value["catId"].'_'.$value["dataId"]]["type"] = 2;
                           
                            break;
                        case 3://hinh anh
                            $item[$value["catId"].'_'.$value["dataId"]] = Album::getAlbumById($value["dataId"]);
                            $item[$value["catId"].'_'.$value["dataId"]]["type"] = 3;
                           
                            break;*/
                    }
                }else{
                    $data_detail = App::getInfoApp($value["dataId"]);
                    $item['0_'.$value["dataId"]] = $data_detail;
                    $item['0_'.$value["dataId"]]["type"] = $data_detail["type"];
                    $item['0_'.$value["dataId"]]["categoryId"] = 0;
                }
            }
            $a = array($type_display,$layout,$item);
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }
        else
        {
            $a=$cache;
        }
        return $a;
    }

}
